package com.c7.bm.mybatis.service.impl;

import com.c7.bm.mybatis.model.C7Activity;
import com.c7.bm.mybatis.model.C7ActivityImages;
import com.c7.bm.mybatis.service.ActivityImagesService;
import com.c7.bm.mybatis.service.ActivityService;
import com.c7.bm.mybatis.util.FileUtil;
import com.c7.bm.mybatis.util.PropertyReader;
import com.c7.bm.mybatis.util.ResponseWrapper;
import com.fasterxml.jackson.databind.deser.Deserializers;
import net.glxn.qrgen.core.image.ImageType;
import net.glxn.qrgen.javase.QRCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.provider.ExampleProvider;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * 活动实现类
 *
 * @author <a href="mailto:chenjunwen@xiaomagouche.com">Duke(陈俊文)</a>
 * @create 17/4/16  上午2:38
 */
@Service
public class ActivityServiceImpl extends BaseService<C7Activity> implements ActivityService {

    /**
     * tomcat虚拟映射路径地址
     */
    private final static String rootPath = "/activity";

    private final static String rootUrl = "http://www.c7image.com/mobile.html";
    @Autowired
    private ActivityImagesService imagesService;

    /**
     * 返回活动详情
     *
     * @return
     */
    @Override
    public List<C7Activity> getActivityList() {
        Example example = new Example(C7Activity.class);
        example.setOrderByClause("id DESC");
        List<C7Activity> list = this.selectByExample(example);
        for (C7Activity activity : list) {
            List<C7ActivityImages> imagesList = imagesService.getAllImagesList(activity.getId());
            activity.setImageCount(imagesList.size());
        }
        return list;
    }

    @Override
    public ResponseWrapper uploadLogo(MultipartFile image, Long activityId) {
        ResponseWrapper responseWrapper = new ResponseWrapper();
        String imagePath = null;
        String fileName = null;
        String filePath = PropertyReader.get("imageFilePath", "file.properties");
        String ptFilePath = PropertyReader.get("logoFilePath", "file.properties");
        String activityImagePath = null;

        try {
            //首屏图片
            if (image != null && image.getSize() > 0) {
                imagePath = filePath + ptFilePath;
                fileName = FileUtil.getFileName(FileUtil.AT_NAME) + FileUtil.getSuffix(image);
                FileUtil.uploadFiles(image, imagePath, fileName);
                activityImagePath = ptFilePath + fileName;//路径存入数据库
                C7Activity c7Activity = this.selectByKey(activityId);
                c7Activity.setLogoPath(rootPath + activityImagePath);//tomcat虚拟映射路径地址需要加上否则无法显示
                this.updateNotNull(c7Activity);
                responseWrapper.setStatus(0);
                responseWrapper.addAttribute("activity", c7Activity);
            }
        } catch (Exception e) {
            responseWrapper.setStatus(1);
            responseWrapper.setMsg(e.toString());
        }
        return responseWrapper;
    }

    /**
     * 生成二维码图片
     *
     * @return
     */
    @Override
    public C7Activity generateQrActivity(Long activityId) throws IOException {
        C7Activity c7Activity = this.selectByKey(activityId);
        String fileName = FileUtil.getFileName(FileUtil.QR_NAME) + ".jpg";
        String filePath = PropertyReader.get("imageFilePath", "file.properties")
                + PropertyReader.get("qrFilePath", "file.properties");
        ByteArrayOutputStream out = QRCode.from(rootUrl + "?activityId=" + c7Activity.getId()).to(ImageType.PNG).withSize(1024, 1024).stream();
        FileOutputStream fileOutputStream = new FileOutputStream(new File(filePath + fileName));

        fileOutputStream.write(out.toByteArray());

        if (fileOutputStream != null) {
            fileOutputStream.flush();
            fileOutputStream.close();
        }
        String qrPath = rootPath + PropertyReader.get("qrFilePath", "file.properties") + fileName;
        c7Activity.setQrPath(qrPath);
        this.updateAll(c7Activity);
        return c7Activity;
    }
}
